package com.flyqiu.flow.starter;

import com.flyqiu.flow.api.FlyQiuFlowEngine;
import com.flyqiu.flow.core.cache.DefaultFlyQiuFlowLocalCache;
import com.flyqiu.flow.core.script.DefaultJuelFlyQiuFlowScriptActuator;
import com.flyqiu.flow.model.format.flowable.FlyQiuFlowFlowableBpmnFormat;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@MapperScan(basePackages = "com.flyqiu.flow.mapper")
@AutoConfigureAfter({FlyQiuFlowManageAutoConfig.class, FlyQiuSpringFlowEngine.class})
public class FlyQiuFlowAutoConfig {

    public FlyQiuFlowAutoConfig() {
//        log.info("初始化流程自动配置");
    }


    /**
     * 创建默认FlowableBpmnFormat 格式化器
     *
     * @return {@link FlyQiuFlowFlowableBpmnFormat}
     */
    @Bean
    @ConditionalOnBean(value = {FlyQiuFlowEngine.class})
    @ConditionalOnMissingBean(FlyQiuFlowFlowableBpmnFormat.class)
    public FlyQiuFlowFlowableBpmnFormat flyQiuFlowFlowableBpmnFormat() {
        return new FlyQiuFlowFlowableBpmnFormat();
    }

    /**
     * 默认缓存器
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(DefaultFlyQiuFlowLocalCache.class)
    @ConditionalOnBean(value = {FlyQiuFlowEngine.class})
    public DefaultFlyQiuFlowLocalCache defaultFlyQiuFlowLocalCache() {
        return new DefaultFlyQiuFlowLocalCache();
    }


    /**
     * 默认脚本执行器
     *
     * @param applicationContext 上下文
     * @return {@link DefaultJuelFlyQiuFlowScriptActuator}
     */
    @Bean
    @ConditionalOnBean(value = {FlyQiuFlowEngine.class})
    @ConditionalOnMissingBean(FlyQiuFlowSpringJuelFlyQiuFlowScriptActuator.class)
    public FlyQiuFlowSpringJuelFlyQiuFlowScriptActuator flyQiuFlowSpringJuelScriptActuator(ApplicationContext applicationContext) {
        return new FlyQiuFlowSpringJuelFlyQiuFlowScriptActuator(applicationContext);
    }


}
